<?



class DBDocumentDoc extends DocumentDoc
{
	/**
	 * what's this
	 * 
	 * @access	public
	 * @param	$sPath=''	string	What's this Parameter ?
	 * @return	array
	 */
	public function DBDocumentDoc($sPath='')
	{
		$this->DocumentDoc( __CLASS__, $sPath ) ;		
		
		$this->arrCodeBlocks['DB Table Link'] = 2 ;
		$this->arrCodeBlocks['Query Condition'] = 2 ;
	}
	/**
	 * what's this
	 * 
	 * @access	public
	 * @static 
	 * @return	string
	 */
	public function GetObjectClass()
	{
		return 'JCAT_DBDocument' ;
	}
	
	static public function GetControllerClassName()
	{
		return 'DBDocument' ;
	}
	
	/**
	 * What's this Method ?
	 * 
	 * @access	protected
	 * @return	void
	 */
	protected function CreateNewDocument()
	{
		// 创建 新的文件 内容 :::::::::::::::::::
		$aTemplate = new JCAT_UIHtml() ;
		$aTemplate->SetVar('TheDoc',$this) ;
		
		// 模版文件
		$sTemplateFile = dirname(__FILE__).'/template.DBDocument.php' ;

		// 产生内容
		$sNewSource = $aTemplate->Display( $sTemplateFile,false) ;
		
		// 调用 父类 创建一个空文件
		parent::CreateNewDocument() ;
		
		// 写入新文件
		file_put_contents($this->GetPath(),$sNewSource) ;
	}
	
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @static 
	 * @return	string
	 */
	public function GetObjctName()
	{
		return $this->Get('DocumentName') ;
	}
	
	
	
	// 创建代码 /////////////////////////////////////////////
	


	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function CreateCode_CallParentConstruct()
	{
		$sMainTable = $this->Get('MainTable') ;
		$sMainKey = $this->Get('MainKey') ;
		
		$sTab = str_repeat("\t",2) ;
		$arrCodeLine[] = "{$sTab}\$this->JCAT_DBDocument(__CLASS__,'{$sMainTable}','{$sMainKey}'" ;
		$arrCodeLine[] = "{$sTab}		, array(" ;
		
		// 使用字段
		$arrColumns = $this->Get('UseColumns') ;
		foreach ($arrColumns as $arrRow)
		{
			$sColumnName = trim($arrRow[TreeUseColumns::CLM_NAME]) ;
			if( empty($sColumnName) )
			{
				continue ;
			}
			$arrCodeLine[] = "{$sTab}			'{$sColumnName}'," ;
		}
		
		$arrCodeLine[] = "{$sTab}		), \$aDB ) ;" ;
		
		
		return implode("\r\n",$arrCodeLine) ;
	}
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	string
	 */
	public function CreateCode_DBTableLink()
	{
		$sTab = str_repeat("\t",2) ;
		$arrCodeLines = array() ;
		$sFactoryVarName = '$sSQLFactory' ;
		$sTableJoinOnVarName = '$sTableJoinOn' ;

		$arrCodeLines[] = "{$sTab}// " . JCAT_Language::Sentence('数据表连接设置') ;
		$arrCodeLines[] = "{$sTab}\$aTableJoin = \$this->GetTableJoin() ; " ;
		$arrCodeLines[] = "{$sTab}{$sFactoryVarName} = \$aTableJoin->GetFactory() ; " ;
		$arrCodeLines[] = "{$sTab}{$sTableJoinOnVarName} = \$aTableJoin->GetTableJoin() ; " ;
		$arrCodeLines[] = '' ; // 空一行
		
		// 表连接 :::::::::
		$arrDBTableLink = $this->Get('TableLink') ;
		foreach ($arrDBTableLink as $arrLink)
		{
			$arrCodeLines[] = "{$sTab}// 连接数据表 {$arrLink[TreeDBTableLink::CLM_LEFT]} 和 {$arrLink[TreeDBTableLink::CLM_RIGHT]} ::::::::::" ;
			
			// 添加左表
			$arrLeftTables = explode(',',$arrLink[TreeDBTableLink::CLM_LEFT]) ;			
			foreach ($arrLeftTables as $sTableName)
			{
				$sTableName = trim($sTableName) ;
				$arrCodeLines[] = "{$sTab}\$aTableJoin->AddTable('{$sTableName}') ; " ;
			}
			
			// 添加右表
			$arrRightTables = explode(',',$arrLink[TreeDBTableLink::CLM_RIGHT]) ;
			foreach ($arrRightTables as $sTableName)
			{
				$sTableName = trim($sTableName) ;
				$arrCodeLines[] = "{$sTab}\$aTableJoin->AddTable('{$sTableName}',false) ; " ;
			}
			
			// 表连接条件组 :::::::::
			$arrGroups = isset($arrLink[JCAT_UIGtkCtrlTree::KEY_CHILDREN])?
							$arrLink[JCAT_UIGtkCtrlTree::KEY_CHILDREN] : array() ;
			foreach ($arrGroups as $nIdx=>$arrTheGroup)
			{
				$sGroupVarName = '$aConditionGroup'.$nIdx ;
				$arrCodeLines[] = $this->CreateCode_SQLConditionGroup($arrTheGroup,$sGroupVarName,$sFactoryVarName,$sTableJoinOnVarName) ;
				$arrCodeLines[] = "{$sTab}{$sTableJoinOnVarName}->AddCondition( {$sGroupVarName} ) ;" ;
			}
		}

		return implode("\r\n",$arrCodeLines) ;
	}

	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	string
	 */
	public function CreateCode_QueryCondition()
	{
		$sTab = str_repeat("\t",2) ;
		$arrCodeLines = array() ;
		$sFactoryVarName = '$sSQLFactory' ;
		$sSubSQLWhereVarName = '\$aSubSQLWhere' ;

		$arrCodeLines[] = "{$sTab}// " . JCAT_Language::Sentence('查询条件设置') ;
		$arrCodeLines[] = "{$sTab}{$sSubSQLWhereVarName} = \$this->GetSubSQLWhere() ; " ;
		$arrCodeLines[] = "{$sTab}{$sFactoryVarName} = \$aTableJoin->GetFactory() ; " ;
		$arrCodeLines[] = '' ; // 空一行
		
		// 表连接 :::::::::
		$arrDBQueryCondition = $this->Get('QueryCondition') ;
		foreach ($arrDBQueryCondition as $nIdx=>$arrConditionGroup)
		{
			$sGroupVarName = '$aConditionGroup'.$nIdx ;
			$arrCodeLines[] = $this->CreateCode_SQLConditionGroup($arrConditionGroup,$sGroupVarName,$sFactoryVarName,$sSubSQLWhereVarName) ;
			$arrCodeLines[] = "{$sTab}{$sSubSQLWhereVarName}->AddCondition( {$sGroupVarName} ) ;" ;
		}

		return implode("\r\n",$arrCodeLines) ;
	}
	
	
	/**
	 * what's this
	 * 
	 * @access	private
	 * @return	void
	 */
	private function CreateCode_SQLConditionGroup(array $arrGroup, $sGroupVarName, $sFactoryVarName, $sParentGroupVarName='null')
	{
		$sTab = str_repeat("\t",2) ;
		$arrCodeLines = array() ;
		$arrCodeLines[] = "{$sTab}// 条件组" ;
		
		$sLogic = strstr($arrGroup[TreeDBTableLink::CLM_EXPRESSION],JCAT_Language::Sentence('与')) ?
					'true': 'false' ;
		
		// 创建条件组
		$arrCodeLines[] = "{$sTab}{$sGroupVarName} = {$sFactoryVarName}->CreateSubSQLConditionGroup({$sLogic},{$sParentGroupVarName}) ; " ;
		
		// 历遍添加组内条件
		foreach ($arrGroup[JCAT_UIGtkCtrlTree::KEY_CHILDREN] as $nIdx=>$arrMember)
		{
			// 条件表达式
			if( $arrMember[TreeDBTableLink::CLM_TYPE]==TreeDBTableLink::TYPE_EXPRESSION )
			{
				$sLeft = trim($arrMember[TreeDBTableLink::CLM_LEFT]) ;
				$sOperator = trim($arrMember[TreeDBTableLink::CLM_OPERATOR]) ;
				$sRight = trim($arrMember[TreeDBTableLink::CLM_RIGHT]) ;
				$sRightB = trim($arrMember[TreeDBTableLink::CLM_RIGHT_B]) ;
				if(!$sRightB)
				{
					$sRightB = 'null' ;
				}
				else 
				{
					$sRightB = "\"{$sRightB}\"" ;
				}
				
				$arrCodeLines[] = "{$sTab}\$aCondition = {$sFactoryVarName}->CreateSubSQLCondition({$sGroupVarName},\"{$sLeft}\",\"{$sRight}\",'{$sOperator}',{$sRightB}) ; " ;
				$arrCodeLines[] = "{$sTab}{$sGroupVarName}->AddCondition(\$aCondition) ;" ;
			}
			
			// 下级 条件组
			else if( $arrMember[TreeDBTableLink::CLM_TYPE]==TreeDBTableLink::TYPE_GROUP ) 
			{
				// 下级 条件组 对象变量名
				$sChildGroupVarName = $sGroupVarName.$nIdx ;
				
				// 递归处理
				$arrCodeLines[] = $this->CreateCode_SQLConditionGroup(&$arrMember,$sChildGroupVarName,$sFactoryVarName,$sGroupVarName) ;
				$arrCodeLines[] = "{$sTab}{$sGroupVarName}->AddCondition( {$sChildGroupVarName} ) ;" ;
			}
			
			// 异常
			else 
			{
				JCAT_ASSERT::ASSERT_(0,'!?') ;
			}
		}

		return implode("\r\n",$arrCodeLines) ;
	}
	

}

JCAT_Package::ImportClass('JCAT_UIHtml',JCAT::PACKAGE_UIHTML) ;
?>